define(function (require, exports, module) {
	var Grid = require("grid");
	var aw = require("ajaxwrapper");
	var Dialog = require("dialog");
	var tools = require("tools");
	
    var instance = {
        initComponent: function (params,widget) {
        	var component = new Grid({
                parentNode: ".J-grid", 
                autoRender:false,
                model : {
                    url : "api/checkindinner/queryall",
                    params: function() {
                    	var subnav=widget.get("subnav");
                        return {
                        	"startDate":subnav.getValue("dateRange").start,
     						"endDate":subnav.getValue("dateRange").end,
     						"status":subnav.getValue("checkStatus"),
     						"checkInImplement.groupReserve.planItemGroup.servicePoint":subnav.getValue("servicePoint"),
     						fetchProperties: "*,checkInImplement.version," +
								"checkInImplement.pkCIImplement," +
								"checkInImplement.status," +
								"checkInImplement.signUpNum," +
								"checkInImplement.dataSource.*," +
								"status.*," +
								"checkInImplement.groupReserve.*," +
								"checkInImplement.groupReserve.planItemGroup.allianceOrg.*," +
								"checkInImplement.groupReserve.planItemGroup.planItem.*," +
								"checkInImplement.groupReserve.planItemGroup.planItem.destination.*," +
								"checkInImplement.groupReserve.planItemGroup.servicePoint.pkServicePoint," +
								"checkInImplement.groupReserve.planItemGroup.servicePoint.name," +
								"checkInImplement.groupReserve.planItemGroup.signUps.version," +
								"checkInImplement.nonMemGroupReserve.nonMemberGroup.*," +
								"checkInImplement.nonMemGroupReserve.nonMemberGroup.groupUnit," +
								"checkInImplement.nonMemGroupReserve.nonMemberGroup.servicePoint.name," +
								"checkInImplement.nonMemGroupReserve.nonMemberGroup.signUpType.*," +
								"checkInImplement.nonMemGroupReserve.nonMemberGroup.operator.servicePoint.name," +
								"checkInImplement.nonMemGroupReserve.nonMemberGroup.signUps.version"
                        };
                    },
                    columns : [{
                    	name : "group",
                    	label : "团/散客",
                    	format : function(value, row){
                    		if(row.checkInImplement.dataSource.key == "GroupReserve"){
        						return "老人团";
        					}else{
        						return "散客";
        					}
                    	}
                    },{
                    	name:"checkInImplement.groupReserve",
        				label:"组团单位/预约人",
        				format: function(value,row){
        					if(row.checkInImplement.dataSource.key == "GroupReserve"){
        						if(value.planItemGroup.allianceOrg){
        							return value.planItemGroup.allianceOrg.name;
        						}else{
        							return value.planItemGroup.servicePoint.name
        						}
        					}else{
    							return row.checkInImplement.nonMemGroupReserve.nonMemberGroup.contactName;
        					}
        				}
        			},{
        				name:"checkInImplement.groupReserve.planItemGroup.planItem.destination.name",
        				label:"目的地",
        				format : function(value, row){
        					if(row.checkInImplement.dataSource.key == "GroupReserve"){
        						return value;
        					}else{
        						return row.checkInImplement.nonMemGroupReserve.nonMemberGroup.servicePoint.name;
        					}
        				}
					},{
						name:"checkInImplement.groupReserve.checkInDate",
						label:"入住日期",
						format: function(value, row){
							if(row.checkInImplement.dataSource.key == "GroupReserve"){
        						return moment(value).format("YYYY-MM-DD");
        					}else{
        						return moment(row.checkInImplement.nonMemGroupReserve.nonMemberGroup.checkInDate).format("YYYY-MM-DD");
        					}
						}
					},{						
						name:"checkInImplement.groupReserve.checkOutDate",
        				label:"退住日期",
        				format:function(value,row){
        					if(row.checkInImplement.dataSource.key == "GroupReserve"){
        						return moment(value).format("YYYY-MM-DD");
        					}else{
        						return moment(row.checkInImplement.nonMemGroupReserve.nonMemberGroup.checkOutDate).format("YYYY-MM-DD");
        					}
        				}
					},{
        				name:"checkInImplement.signUpNum",
        				label:"报名人数"
        			},{
						name:"status.value",
						label:"状态"
					},{
						name:"operate",
						label:"操作",
						format:"button",
						formatparams:[{
							key:"edit",
							text:"落实",
							show:function(value,data){
								if (data.status.key == "Pending") {
									return true;
			                    } else {
			                        return false;
			                    }
							},
							handler:function(index,data,rowEle){
								
	        						if(data.checkInImplement.status.key=="Edited" || data.checkInImplement.status.key=="Initial"){
	        							Dialog.alert({
						    				content:"此数据未准备，不得落实！"
						            	});
	        						}else{
//	        							roomHandler.handleSetBtnClick(widget,data);
	        							var form = widget.get("form");
	        							var gridEnd = widget.get("gridEnd");
	    								widget.show([".J-form",".J-gridEnd"]).hide([".J-grid"]);
	    								widget.get("subnav").show(["back"]).hide(["save","search","checkStatus","servicePoint","dateRange"]);
	    								
	    								form.reset();
	    								form.setValue("description", data.checkInImplement.nonMemGroupReserve!=null?data.checkInImplement.nonMemGroupReserve.nonMemberGroup.description:data.checkInImplement.groupReserve.description);
	    								form.setValue("version", data.version);
	    								form.setValue("pkCheckInDinner", data.pkCheckInDinner);
	    								form.setValue("haveDinner", data.haveDinner);
	    								form.setValue("breakfast", data.breakfast);
	    								form.setValue("breakfastConfirm", data.breakfastConfirm);
	    								form.setValue("lunch", data.lunch);
	    								form.setValue("lunchConfirm", data.lunchConfirm);
	    								form.setValue("supper", data.supper);
	    								form.setValue("supperConfirm", data.supperConfirm);
	    								form.setValue("checkInImplement", data.checkInImplement.pkCIImplement);
	    								if(data.checkInImplement.groupReserve==null){
	    									form.setValue("groupReserve",data.checkInImplement.nonMemGroupReserve.nonMemberGroup.contactName);
	    									form.setValue("destination",data.checkInImplement.nonMemGroupReserve.nonMemberGroup.servicePoint.name);
	    									form.setValue("checkInDate",moment(data.checkInImplement.nonMemGroupReserve.nonMemberGroup.checkInDate).format("YYYY-MM-DD"));
	    									form.setValue("checkOutDate",moment(data.checkInImplement.nonMemGroupReserve.nonMemberGroup.checkOutDate).format("YYYY-MM-DD"));
	    									form.setValue("signUpNum",data.checkInImplement.signUpNum);
	    								}else{
	    									form.setValue("groupReserve",data.checkInImplement.groupReserve.planItemGroup.servicePoint == null ?
	    											data.checkInImplement.groupReserve.planItemGroup.allianceOrg.name : data.checkInImplement.groupReserve.planItemGroup.servicePoint.name);
	    									form.setValue("destination",data.checkInImplement.groupReserve.planItemGroup.planItem.destination.name);
	    									form.setValue("checkInDate",moment(data.checkInImplement.groupReserve.checkInDate).format("YYYY-MM-DD"));
	    									form.setValue("checkOutDate",moment(data.checkInImplement.groupReserve.checkOutDate).format("YYYY-MM-DD"));
	    									form.setValue("signUpNum",data.checkInImplement.signUpNum);
	    								}
	    								
	    								form.setDisabled(true);
	    								
	    								widget.set("pkCheckInDinner",data.pkCheckInDinner);
	    								gridEnd.refresh();
	    								if(data.breakfast){
	    									$("div[el-data-component=breakfastConfirm]").removeClass("hidden")
	    								}else{
	    									$("div[el-data-component=breakfastConfirm]").addClass("hidden")
	    								}
	    								if(data.lunch){
	    									$("div[el-data-component=lunchConfirm]").removeClass("hidden")
	    								}else{
	    									$("div[el-data-component=lunchConfirm]").addClass("hidden")
	    								}
	    								if(data.supper){
	    									$("div[el-data-component=supperConfirm]").removeClass("hidden")
	    								}else{
	    									$("div[el-data-component=supperConfirm]").addClass("hidden")
	    								}
	        						}
							}	
						}]
					}]
                }
            });
            widget.set("grid",component);
        }
    };
    module.exports = instance;
});